<?php
        
        class PView
        {
        	var $puzzle;
        	
        	var $name;
        	var $extension;
        	
        	var $pdefinition;
                var $params;
                var $initialized;
                
                var $view_type;
                
                
                function PView(&$owner, $extension, $view)
                {
                	$this->puzzle = $owner;
                	
                	$this->name = $view;
                        $this->extension = $extension;
                        
                        $this->pdefinition = array();
			//$this->params = array();
			
			$this->view_type = "sub";
			
			$this->defineView();
                }
                
                /*
                function setParameter($pname, $pvalue)
                {
                        $this->params[$pname] = $pvalue;
                }
                
                function getParameter($pname)
                {
                        return $this->params[$pname];
                }
                */
                
                function defineView()
                {
                	// save previous extension and set the own one
                	$prev_extension = $this->puzzle->PEM->current_extension;
                	$this->puzzle->PEM->useExtension($this->extension);
                	
                	// load the def.php file of the view
                	$DS = $this->puzzle->config['DS'];
                        include $this->puzzle->config['dir_ext'] . $DS . $this->extension . $DS . $this->puzzle->config['dir_ext_src'] . $DS . $this->puzzle->config['dir_ext_views'] . $DS . $this->name . $DS . "def.php";
                        
                        // restore the previous extension as current one
                        $this->puzzle->PEM->useExtension($prev_extension);
                }
                
                function initialize()
                {
                	// save previous extension and set the own one
                	$prev_extension = $this->puzzle->PEM->current_extension;
                	$this->puzzle->PEM->useExtension($this->extension);
                	
                	// load the parameters for the view
                	$this->loadParameters();
                	
                	// load the init.php file of the view and set it as initialized
                	$DS = $this->puzzle->config['DS'];
                        include $this->puzzle->config['dir_ext'] . $DS . $this->extension . $DS . $this->puzzle->config['dir_ext_src'] . $DS . $this->puzzle->config['dir_ext_views'] . $DS . $this->name . $DS . "init.php";
                        $this->initialized = true;
                        
                        // restore the previous extension as current one
                        $this->puzzle->PEM->useExtension($prev_extension);
                }
                
                function render()
                {
                	// save previous extension and set the own one
                	$prev_extension = $this->puzzle->PEM->current_extension;
                	$this->puzzle->PEM->useExtension($this->extension);
                	
                        $DS = $this->puzzle->config['DS'];
                	
                        include $this->puzzle->config['dir_ext'] . $DS . $this->extension . $DS . $this->puzzle->config['dir_ext_src'] . $DS . $this->puzzle->config['dir_ext_views'] . $DS . $this->name . $DS . "render.php";
                        $this->initialized = true;

			// restore the previous extension as current one
			$this->puzzle->PEM->useExtension($prev_extension);
                }
                
                function defineParameter($name, $set = true, $post = false, $get = false, $required = false, $default = null, $type = null)
                {
                	$def['name'] = $name;
                	$def['set'] = $set;
                	$def['post'] = $post;
                	$def['get'] = $get;
                	$def['required'] = $required;
                	$def['default'] = $default;
                	$def['type'] = $type;
                	
                	$this->pdefinition[] = $def;
                }
                
                function loadParameters()
                {
                	
                	foreach ($this->pdefinition as $def)
                	{
                		$value = null;
                		$varname = $def['name'];
                		
                		if ($def['set'] == true)				// if set is accepted, set the value to
                		{
                		      	$value = $this->params->$varname;
		        	}
		        	else							// if not...
		        	{
		        		$value = null;					// delete the value is set is not accepted
		        	}
		        	
		        	if ($value === null && $def['post'])			// if post is accepted, take its value
				{
					$value = $_POST[$def['name']];
				}
				
				if ($value === null && $def['get'])			// if GET is accepted take the value
				{
					$value = $_GET[$def['name']];
				}
		        	
		        	if ($value === null)					// if still without a value, assign a default one if exists
		        	{
		        		$value = $def['default'];
		        	}
		        	
		        	if ($def['required'] == true && $value === null)	// if value required and is null, then die
		        	{
		        		die("PHP-Puzzle :: Loading view " . $this->name . " :: Required parameter '" . $def['name']. "' is not set");
		        	}
		        	
		        	$this->params->$varname = $value;
                	}
                }
        }
        
?>
